﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Basic;
using Interface;

namespace Client
{
    public partial class UIManager
    {
        #region static creator cacher
        private static Cacher<UIData> s_DataCacher;
        private static Dictionary<string, Func<UIWindow>> s_dictWindowCreator;
        private static Dictionary<string, Func<UIWidget>> s_dictWidgetCreator;
        private static Dictionary<string, Cacher<UIWindow>> s_dictWindowCacher;
        private static Dictionary<string, Cacher<UIWidget>> s_dictWidgetCacher;

        private UIData AllocUIData()
        {
            return s_DataCacher.Alloc();
        }
        private void OnUIDataUseEnd(UIData request)
        {
            request.CancelShow = false;
            request.Base = null;
            request.Window = null;
            request.Widget = null;
            s_DataCacher.OnUseEnd(request);
        }

        private UIWindow AllocWindow(string uiKey)
        {
            var creator = s_dictWindowCreator.TryGet(uiKey);
            if (creator == null)
            {
                Log.Error("UIManager.AllocWindow creator not exist: {0}", uiKey);
                return null;
            }

            Cacher<UIWindow> cacher;
            if (!s_dictWindowCacher.TryGetValue(uiKey, out cacher))
            {
                cacher = new Cacher<UIWindow>(1, 1, 1, creator);
                s_dictWindowCacher[uiKey] = cacher;
            }

            var window = cacher.Alloc();
            window.ResetForUse();
            return window;
        }
        private void OnWindowUseEnd(UIWindow window)
        {
            if (window == null)
            {
                Log.Error("UIManager.OnWindowUseEnd window is null");
                return;
            }

            Cacher<UIWindow> cacher;
            if (s_dictWindowCacher.TryGetValue(window.UIKey, out cacher))
            {
                cacher.OnUseEnd(window);
            }
            else
            {
                Log.Error("UIManager.OnWindowUseEnd no cacher for uikey: {0}", window.UIKey);
            }
        }

        private UIWidget AllocWidget(string uiKey)
        {
            var creator = s_dictWidgetCreator.TryGet(uiKey);
            if (creator == null)
            {
                Log.Error("UIManager.AllocWidget creator not exist: {0}", uiKey);
                return null;
            }

            Cacher<UIWidget> cacher;
            if (!s_dictWidgetCacher.TryGetValue(uiKey, out cacher))
            {
                cacher = new Cacher<UIWidget>(1, 1, 1, creator);
                s_dictWidgetCacher[uiKey] = cacher;
            }

            var widget = cacher.Alloc();
            widget.ResetForUse();
            return widget;
        }
        private void OnWidgetUseEnd(UIWidget widget)
        {
            if (widget == null)
            {
                Log.Error("UIManager.OnWidgetUseEnd widget is null");
                return;
            }

            Cacher<UIWidget> cacher;
            if (s_dictWidgetCacher.TryGetValue(widget.UIKey, out cacher))
            {
                cacher.OnUseEnd(widget);
            }
            else
            {
                Log.Error("UIManager.OnWidgetUseEnd no cacher for uikey: {0}", widget.UIKey);
            }
        }
        #endregion

        private static void InitCacher()
        {
            s_DataCacher = new Cacher<UIData>(8, 4, 8, () => { return new UIData(); });

            s_dictWindowCreator = new Dictionary<string, Func<UIWindow>>
            {
                { UIWindowLogin.UIKEY, () => { return new UIWindowLogin(); } }
            };
            s_dictWidgetCreator = new Dictionary<string, Func<UIWidget>>
            {
            };

            s_dictWindowCacher = new Dictionary<string, Cacher<UIWindow>>(64);
            s_dictWidgetCacher = new Dictionary<string, Cacher<UIWidget>>(32);
        }


        /// <summary>
        /// ui使用完毕
        /// </summary>
        /// <param name="ui"></param>
        public void OnUIBaseUseEnd(UIBase ui)
        {
            Log.Debug("UIManager.OnUIBaseUseEnd {0}", ui != null ? ui.UIKey : "null");
            if (ui == null)
            {
                Log.Error("UIManager.OnUIBaseUseEnd uibase is null");
                return;
            }

            if (ui is UIWindow)
            {
                OnWindowUseEnd(ui as UIWindow);
            }
            else if (ui is UIWidget)
            {
                OnWidgetUseEnd(ui as UIWidget);
            }
            else
            {
                Log.Error("UIManager.OnUIBaseUseEnd not support uibase {0}", ui.GetType().FullName);
            }
        }
    }
}
